package lk.service.blogspringboot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lk.service.blogspringboot.controller.basic.BasicController;
import lk.service.blogspringboot.domain.ArticleShareDetail;
import lk.service.blogspringboot.service.ArticleShareDetailService;
import lk.service.blogspringboot.util.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文章分享详细信息表(ArticleShareDetail)表控制层
 *
 * @author LK
 * @since 2024-08-22 10:33:41
 */
@RestController
@RequestMapping("articleShareDetail")
public class ArticleShareDetailController extends BasicController {
    /**
     * 服务对象
     */
    @Resource
    private ArticleShareDetailService articleShareDetailService;

    /**
     * 全查询   带有分页
     *
     * @param articleShareDetail 筛选条件
     * @return 查询结果
     */
    @GetMapping
    public R queryAll(ArticleShareDetail articleShareDetail) {
        // 获取前端pageNum和pageSize
        int pageNum = articleShareDetail.getPageNum();
        int pageSize = articleShareDetail.getPageSize();
        // 开始分页
        PageHelper.startPage(pageNum, pageSize);
        R r = this.articleShareDetailService.queryAll(articleShareDetail);
        PageInfo<ArticleShareDetail> pageInfo = new PageInfo<>((List<ArticleShareDetail>) r.getData());
        setReturnData(pageInfo.getTotal(), r);
        return r;
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R queryById(@PathVariable("id") Integer id) {
        return this.articleShareDetailService.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param articleShareDetail 实体
     * @return 新增结果
     */
    @PostMapping
    public R add(@RequestBody ArticleShareDetail articleShareDetail) {
        return this.articleShareDetailService.insert(articleShareDetail);
    }

    /**
     * 编辑数据
     *
     * @param articleShareDetail 实体
     * @return 编辑结果
     */
    @PutMapping
    public R edit(@RequestBody ArticleShareDetail articleShareDetail) {
        return this.articleShareDetailService.update(articleShareDetail);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("{id}")
    public R deleteById(@PathVariable("id") Integer id) {
        return this.articleShareDetailService.deleteById(id);
    }


    /**
     * 批量删除数据
     *
     * @param idArr 主键数组
     * @return 删除是否成功
     */
    @PostMapping("/deleteBatch")
    public R deleteByIdBatch(@RequestBody List<Integer> idArr) {
        idArr.forEach((id) -> this.articleShareDetailService.deleteById(id));
        return R.ok();
    }

}
